from app import db
from datetime import datetime

class BloodTest(db.Model):
    __tablename__ = 'blood_tests'
    
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
    test_date = db.Column(db.DateTime, default=datetime.utcnow)
    
    # 血糖相关
    fasting_blood_sugar = db.Column(db.Float)  # 空腹血糖 (mmol/L)
    postprandial_blood_sugar = db.Column(db.Float)  # 餐后2小时血糖 (mmol/L)
    hba1c = db.Column(db.Float)  # 糖化血红蛋白 (%)
    
    # 血脂相关
    total_cholesterol = db.Column(db.Float)  # 总胆固醇 (mmol/L)
    triglycerides = db.Column(db.Float)  # 甘油三酯 (mmol/L)
    hdl_cholesterol = db.Column(db.Float)  # 高密度脂蛋白 (mmol/L)
    ldl_cholesterol = db.Column(db.Float)  # 低密度脂蛋白 (mmol/L)
    
    # 肝功能
    alt = db.Column(db.Float)  # 谷丙转氨酶 (U/L)
    ast = db.Column(db.Float)  # 谷草转氨酶 (U/L)
    
    def to_dict(self):
        return {
            'id': self.id,
            'user_id': self.user_id,
            'test_date': self.test_date.isoformat(),
            'fasting_blood_sugar': self.fasting_blood_sugar,
            'postprandial_blood_sugar': self.postprandial_blood_sugar,
            'hba1c': self.hba1c,
            'total_cholesterol': self.total_cholesterol,
            'triglycerides': self.triglycerides,
            'hdl_cholesterol': self.hdl_cholesterol,
            'ldl_cholesterol': self.ldl_cholesterol,
            'alt': self.alt,
            'ast': self.ast
        }